package com.lagou.edu.controller;


import com.lagou.edu.api.AuthCodeServiceApi;
import com.lagou.edu.feign.CodeClient;
import com.lagou.edu.pojo.Token;
import com.lagou.edu.pojo.User;
import com.lagou.edu.service.UserService;
import com.lagou.edu.utils.Constants;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

//    @Autowired
//    private CodeClient codeClient;
    @Reference
    private AuthCodeServiceApi authCodeServiceApi;

    /***
     * 注册
     */
    @GetMapping("/register/{email}/{password}/{code}")
    public String register(@PathVariable @NotNull String email,
                           @PathVariable @NotNull String password,
                           @PathVariable @NotNull String code) {
        // 检查是否已经注册，已经注册则返回失败
        User userExists = userService.findByEmail(email);
        if (userExists != null) {
            return "false";
        }

        // 检查验证码有效性
//        String checkResult = codeClient.validate(email, code);
        String checkResult = authCodeServiceApi.validate(email, code);
        if (!"0".equals(checkResult)) {
            return checkResult;
        }

        // 添加一个用户
        User user = new User();

        user.setEmail(email);
        user.setPassword(password);

        userService.save(user);

        return "0";
    }

    /***
     * 是否已注册,根据邮箱判断,true代表已经注册过,false代表尚未注册
     */
    @GetMapping("/isRegistered/{email}")
    public String isRegistered(@PathVariable @NotNull String email) {
        User user = userService.findByEmail(email);

        return user != null ? "true" : "false";
    }

    /***
     * 登录
     */
    @GetMapping("/login/{email}/{password}")
    public String login(@PathVariable @NotNull String email,
                        @PathVariable @NotNull String password,
                        HttpServletResponse response) {
        String token = userService.login(email, password);
        // 登录失败
        if ("1".equals(token) || "2".equals(token)) {
            return "false";
        }

        // 将生成的token放到cookie中
        Cookie cookie = new Cookie(Constants.KEY_TOKEN,token);
        cookie.setPath("/");
        response.addCookie(cookie);

        return email;
    }

    /***
     * 根据token查找用户名即邮箱
     */
    @GetMapping("/info/{token}")
    public String info(@PathVariable @NotNull String token) {
        Token userToken = userService.findByToken(token);

        if (userToken == null) {
            return "false";
        }

        return userToken.getEmail();
    }
}
